package com.ts.task;

import com.alibaba.fastjson.JSONObject;
import com.ts.mapper.dataOne.TransitMapper;
import com.ts.model.BusRun;
import com.ts.utils.HttpClientUtil;
import com.ts.utils.JsonUtil;
import com.ts.utils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
@ComponentScan
public class TransitTaskScheduler {

    private final TransitMapper transitMapper;

    @Autowired
    public TransitTaskScheduler(TransitMapper transitMapper) {
        this.transitMapper = transitMapper;
    }

    //**************************************************互联网**************************************************

    /**
     *  从互联网获取车辆实时信息，将文件放到FTP前置机上，频率15S
     */
    //@Scheduled(cron = "0 * * * * ?")
    public void taskScheduler(){
        try {
            String a = HttpClientUtil.sendHttpGet("http://61.131.207.68:31007/getBusRunList");
            String version = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            String path = PropertyUtils.getProp("spring.http.multipart.location");
            String fileName = "busrunlist" + version + ".txt";
            File txt=new File(path+fileName);
            if(!txt.exists()){
                txt.createNewFile();
            }
            byte bytes[]=new byte[512];
            bytes=a.getBytes();
            int b=bytes.length;
            FileOutputStream fos=new FileOutputStream(txt);
            fos.write(bytes,0,b);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *  从互联网获取车辆实时信息，将文件放到FTP前置机上，频率15S
     */
    //@Scheduled(cron = "55 * * * * ?")
    public void deleteBusRunFileScheduler(){
        try {
            String oldFilePath = PropertyUtils.getProp("spring.http.multipart.location");
            File oldFile = new File(oldFilePath);
            File[] files = oldFile.listFiles();
            if(null != files && files.length > 0){
                int i = 1;
                for (File file : files) {
                    file.delete();
                    i++;
                }
                System.out.println("本次共删除文件：【"+ i + "】个");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //**************************************************公安网**************************************************

    /**
     *  解析跨域过来的车辆实时信息数据并入库
     */
   // @Scheduled(cron = "40 * * * * ?")
    public void policeBusRunListTask(){
        try {
            long timestamp = System.currentTimeMillis();
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp));
            String oldFilePath = PropertyUtils.getProp("spring.http.multipart.location");
            String newFilePath = PropertyUtils.getProp("spring.http.multipart.location") + timestamp;

            File oldFile = new File(oldFilePath);
            File[] oldFiles = oldFile.listFiles();
            if(null != oldFiles && oldFiles.length > 0){
                //1.创建一个临时文件夹
                File temFile = new File(newFilePath);
                temFile.mkdirs();
                //2.把现有的busrunlist文件都放到临时文件里面
                for (File file : oldFiles ) {
                    if(file.getName().startsWith("busrunlist")) {
//                        Windows下载文件路径
//                        file.renameTo(new File(newFilePath +"\\"+ file.getName()));
//                        Linux下载文件路径
                        file.renameTo(new File(newFilePath +"/"+ file.getName()));
                    }
                }
                //3.遍历新文件夹里面的所有文件
                File newFile = new File(newFilePath);
                File[] newFiles = newFile.listFiles();
                for (File file : newFiles) {
                    //4.解析文件，并将结果入库
                    String jsonString = JsonUtil.readJsonFile(file);
                    List<BusRun> busRuns = JSONObject.parseArray(jsonString, BusRun.class);
                    for (BusRun busRun :
                            busRuns) {
                        transitMapper.insertBusRun(busRun);
                    }
                }
                System.out.println(time+"本次需导入文件【"+ newFiles.length +"】个");

                //3.解析结束后删除临时文件夹和文件
                File[] files = temFile.listFiles();
                if(null != files && files.length >0){
                    for (File file :
                            files) {
                        file.delete();
                    }
                }
                if(temFile.delete()){
                    System.out.println(time+"删除临时文件夹成功");
                }
                System.out.println(time+"车辆实时信息入库完成！！！！！！！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws  Exception{

    }

}
